package com.wxtech.heartbeat.server.protocol;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.wxtech.heartbeat.Context;
import com.wxtech.heartbeat.model.ErrorData;

import io.netty.channel.ChannelHandlerContext;


public class ErrorDecodeResult extends DecodeResult {
	private static Logger logger = Logger.getLogger(ErrorDecodeResult.class);
	private byte[] rawData;
	private String errorMessage;
	private int errorCode;
	
	public ErrorDecodeResult() {
		// TODO Auto-generated constructor stub
	}

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}

	public byte[] getRawData() {
		return rawData;
	}

	public void setRawData(byte[] rawData) {
		this.rawData = rawData;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	@Override
	public void handler(ChannelHandlerContext ctx) {
		// TODO Auto-generated method stub
		logger.info("Error=" + this.getErrorMessage());
		ErrorData ed = new ErrorData();
		ed.setErrorCode(this.getErrorCode());
		ed.setData(this.getRawData());
		try {
			Context.getDataManager().addErrorData(ed);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

}
